package utilities;

public class TestModel implements Archiveable {
	static final String FILENAME = "/TestModel.txt";
	
	TestEntity entity;
	int modelNo;
	
	public TestModel() {
		this.entity = new TestEntity();
		this.modelNo = 1;
	}
	
	
	public Archive save() {
		Archive archive = new JSONArchive();
		
		archive.putClassName("entity", this.entity.getClass().getName());
		archive.putOneRelationship("entity", this.entity);
		
		archive.putAttribute("modelNo", Integer.toString(this.modelNo));
		
		return archive;
	}
	
	public void load(Archive archive) {
		Archive entityArchive = archive.getOneRelationship("entity");
		this.entity = (TestEntity)ModelFactory.getInstance().createInstanceWithClassName(archive.getClassName("entity"));
		this.entity.load(entityArchive);
		
		this.modelNo = Integer.parseInt(archive.getAttribute("modelNo"));
	}
	
	public void saveData() {
		Archiver archiver = new JSONArchiver(FILENAME);
		archiver.save(this);
	}
	
	public void loadData() {
		Archiver archiver = new JSONArchiver(FILENAME);
		archiver.load(this);
	}
	
	
	public static void main(String args[]) {
		TestModel model = new TestModel();
		model.saveData();
		
		TestModel loaded = new TestModel();
		loaded.loadData();
		System.out.println(model);
//		TestEntity entity = new TestEntity();
//		System.out.println(entity.getClass().getName());
	}
}
